package com.nervenets.web.controller.handler.admin.news;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.news.News;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class NewsEditHandler extends SessionHandler<NewsEditParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(NewsEditParams params, HttpServletRequest request) throws Exception {
        News news = new News();
        if (params.getNewsId() > 0) {
            news = userDao.findById(params.getNewsId(), News.class);
            if (null == news) return errorMessage(MessageCode.code_404, "您所编辑的资讯不存在！");
        }
        news.getInfo().setTitle(params.getTitle());
        news.getInfo().setContent(params.getContent());
        news.getInfo().setBigCoverImage(params.getBigCoverImage());
        news.getInfo().setSmallCoverImage(params.getSmallCoverImage());
        news.getInfo().setSource(params.getSource());
        news.setType(params.getNewsType());
        news.setHot(params.isHot());
        news.setQueue(params.getQueue());
        userDao.saveOrUpdate(news);
        return successMessage(news);
    }

    @Override
    public String result() throws Exception {
        return successMessage("资讯信息");
    }
}
